package com.uow.algdemo.material;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class Line extends View
{
	/**
     * This is paint for line and text.
     */
	private final Paint mPaint = new Paint();
	/**
     * This is x for first point.
     */
	private int xPos_start;
	/**
     * This is y for first point.
     */
	private int yPos_start;
	/**
     * This is x for second point.
     */
	private int xPos_end;
	/**
     * This is y for second point.
     */
	private int yPos_end;
	/**
     * This is the color for line.
     */
	private int bgColor = Color.BLACK;
	/**
     * This is the length for line.
     */
	private int length = -1;

	/**
     * @param context The Context the view is running in, through which it can access the current theme, resources, etc.
	 */
	public Line(Context context)
	{
		super(context);
	}
	/**
     * @param context The Context the view is running in, through which it can access the current theme, resources, etc.
	 * @param x x for first point.
     * @param y y for first point.
	 */
	public Line(Context context, int x, int y)
	{
		super(context);
		xPos_start = x;
		yPos_start = y;
	}
	/**
     * @param context The Context the view is running in, through which it can access the current theme, resources, etc.
	 * @param x1 x for first point.
     * @param y1 y for first point.
	 * @param x2 x for second point.
     * @param y2 y for second point.
	 */
	public Line(Context context, int x1, int y1, int x2, int y2)
	{
		super(context);
		xPos_start = x1;
		yPos_start = y1;
		xPos_end = x2;
		yPos_end = y2;
	}
	/**
     * This method set coods for second point.
	 * @param x x for second point.
     * @param y y for second point.
	 */
	public void setEnd(int x, int y)
	{
		xPos_end = x;
		yPos_end = y;
		this.invalidate();
	}
	/**
	 * @param canvas the canvas on which the background will be drawn.
	 */
	public void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		mPaint.setColor(bgColor);
		mPaint.setAntiAlias(true);
		mPaint.setStrokeWidth((float) 3.0);
		canvas.drawLine(xPos_start, yPos_start, xPos_end, yPos_end, mPaint);
		if(length> 0 && length < 100)
		{
			canvas.drawText(String.valueOf(length), (xPos_start + xPos_end) / 2, (yPos_start + yPos_end) / 2, mPaint);
		}
	}
	/**
     * This method set length of line.
	 * @param len length of the line.
	 */
	public void setLength(int len)
	{
		length = len;
		this.invalidate();
	}
	/**
     * This method set color of line to green.
	 */
	public void colorGreen()
	{
		bgColor = Color.GREEN;
		this.invalidate();
	}
	/**
     * This method set color of line to blue.
	 */
	public void colorBlue()
	{
		bgColor = Color.BLUE;
		this.invalidate();
	}
	/**
     * This method set color of line to red.
	 */
	public void colorRed()
	{
		bgColor = Color.RED;
		this.invalidate();
	}
	/**
     * This method set color of line to black.
	 */
	public void colorBlack()
	{
		bgColor = Color.BLACK;
		this.invalidate();
	}
}
